#ifndef CONNECTIONMANAGER_H
#define CONNECTIONMANAGER_H

#include <QObject>
#include <QMap>
#include <QString>
#include <QSqlDatabase>

struct ConnectionConfig {
    QString name;
    QString driver;
    QString host;
    QString database;
    QString username;
    QString password;
    int port;
    
    ConnectionConfig() : port(0) {}
    
    ConnectionConfig(const QString &n, const QString &d, const QString &h,
                     const QString &db, const QString &u, const QString &p, int pt = 0)
        : name(n), driver(d), host(h), database(db), username(u), password(p), port(pt) {}
};

class ConnectionManager : public QObject
{
    Q_OBJECT

public:
    explicit ConnectionManager(QObject *parent = nullptr);
    
    bool saveConnection(const QString &name, const ConnectionConfig &config);
    bool removeConnection(const QString &name);
    ConnectionConfig getConnection(const QString &name) const;
    QStringList getConnectionNames() const;
    bool connectionExists(const QString &name) const;
    
    QSqlDatabase createDatabaseConnection(const QString &name) const;
    QSqlDatabase createDatabaseConnection(const ConnectionConfig &config) const;
    
    bool loadConnections();
    bool saveConnections();
    
private:
    QString getConfigFilePath() const;
    
    QMap<QString, ConnectionConfig> m_connections;
};

#endif // CONNECTIONMANAGER_H